using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;
using Deepcode.Flux.Core.Systems.CMS.TemplateGallery;

public partial class Private_CMS_main_actions_addnew_selectType : Deepcode.Flux.Core.UI.CMS.CMSActionPane
{
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			ContentManager cms = new ContentManager();
			ContentType [] AvailableTypes = null;

			if (this.ParentID == -1)
			{
				// Adding to root
				AvailableTypes = ContentTypes.Types.GetAddToRoot();
			}
			else
			{
				// Adding to an existing piece of content
				ContentObject o = cms.AdxGetByID(this.ParentID);
				AvailableTypes = o.TypeTemplate.GetAllowedChildren();
			}

			if (AvailableTypes != null)
			{
				if (AvailableTypes.Length == 1)
					GotoForm(AvailableTypes[0].AddForm);

				foreach (ContentType t in AvailableTypes)
					fAvailableTypes.Items.Add( new ListItem( t.TypeName, t.TypeID ));
			}
		}
	}
	#endregion
	#region Utility
	/// <summary>
	/// Calculates and redirects to the action addition form
	/// </summary>
	/// <param name="form"></param>
	protected void GotoForm(string form)
	{
		string url = String.Format("{0}{1}&ParentID={2}",
			form,
			form.Contains("?") ? "" : "?",
			ParentID);

		Response.Redirect(url, true);
	}
	#endregion
	#region Interactions
	/// <summary>
	/// Type selected, go forward to the actual editor form itself
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnNext_Click(object sender, EventArgs e)
	{
		if( ! Page.IsValid ) return;
		string selType = fAvailableTypes.SelectedValue;
		ContentType t = ContentTypes.Types.GetByTypeID( selType );
		if( t == null ) return;
		GotoForm( t.AddForm );
	}
	/// <summary>
	/// Cancel clicked - go back to content view listing
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	#endregion
}
